package com.cssw.fyzb.web.controller;

import com.cssw.fyzb.base.web.domain.AjaxResult;
import com.cssw.fyzb.web.dto.DeleteRoleModel;
import com.cssw.fyzb.web.dto.SaveRoleFuncModel;
import com.cssw.fyzb.web.dto.SaveRoleModel;
import com.cssw.fyzb.web.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: yangyiqun
 * @Date: 2020/8/3 10:33
 */

@Api(description = "角色相关接口")
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ApiOperation("查询角色列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleName", value = "角色名称", dataType = "string", paramType = "query", required = false),
            @ApiImplicitParam(name = "page", value = "当前页", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "limit", value = "页大小", dataType = "int", paramType = "query", required = true)
    })
    @GetMapping("list")
    public AjaxResult list(String roleName, int page, int limit) {
        return roleService.list(roleName, page, limit);
    }

    @ApiOperation("保存角色")
    @ApiImplicitParam(name = "model", value = "保存角色参数", dataType = "SaveRoleModel", required = true)
    @PostMapping("save")
    public AjaxResult save(@RequestBody SaveRoleModel model) {
//        saveOperLog("保存角色", model);
        return roleService.save(model.getRoleId(), model.getRoleName(), model.getRoleDesc());
    }

    @ApiOperation("删除角色")
    @ApiImplicitParam(name = "model", value = "删除角色参数", dataType = "DeleteRoleModel", required = true)
    @PostMapping("delete")
    public AjaxResult delete(@RequestBody DeleteRoleModel model) {
//        saveOperLog("删除角色", model);
        return roleService.delete(model.getRoleId());
    }

    @ApiOperation("保存角色功能列表")
    @ApiImplicitParam(name = "model", value = "保存角色功能参数", dataType = "SaveRoleFuncModel", required = true)
    @PostMapping("saveRoleFunc")
    public AjaxResult saveRoleFunc(@RequestBody SaveRoleFuncModel model) {
//        saveOperLog("保存角色功能列表", model);
        return roleService.saveRoleFunc(model.getRoleId(), model.getFuncList());
    }


    @ApiOperation("查询角色功能树")
    @ApiImplicitParam(name = "roleId", value = "角色标识", dataType = "string", paramType = "query", required = true)
    @GetMapping("querySysFuncTreeByRoleId")
    public AjaxResult querySysFuncTreeByRoleId(String roleId) {
        return roleService.querySysFuncTreeByRoleId(Long.valueOf(roleId));
    }


//    private void saveOperLog(String name, Object param){
//        try{
//            operationLogService.save(null, name, JSON.toJSONString(param));
//        }
//        catch (Exception e){
//
//        }
//    }
}
